package com.example.testapplication.ui.mine;

import static me.goldze.mvvmhabit.utils.Utils.getContext;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import androidx.lifecycle.MutableLiveData;

import com.example.testapplication.BR;
import com.example.testapplication.R;
import com.example.testapplication.event.Event;
import com.example.testapplication.ui.login.LoginFragmentViewModel;
import com.example.testapplication.view.CustomPopup;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MineViewModel extends BaseViewModel {
    private Disposable mSubscription;
    public MineViewModel(@NonNull Application application) {
        super(application);
        for (int i = 0; i < txts.length; i++) {
            observableList.add(new MineItemViewModel(MineViewModel.this,txts[i]));
        }
    }

    //头像的绑定
    public ObservableField<String> avatar = new ObservableField<>("");

    //添加ObservableList
    public ObservableList<MineItemViewModel> observableList = new ObservableArrayList<>();

    //添加ItemBinding
    public ItemBinding<MineItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_mine);

    public String[] txts = new String[]{"设置","个人资料","选择图片","网页","气泡"};

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();



    public class UIChangeObservable {
        //点击事件观察者
        public SingleLiveEvent<Integer> pClickEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onMoreClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.pClickEvent.setValue(1);
        }
    });

    public BindingCommand onAvatarClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.pClickEvent.setValue(3);
        }
    });

    //注册RxBus
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        mSubscription = RxBus.getDefault().toObservable(Event.class)
                .subscribe(new Consumer<Event>() {
                    @Override
                    public void accept(Event s) throws Exception {
                        avatar.set(s.msg);
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }

    //移除RxBus
    @Override
    public void removeRxBus() {
        super.removeRxBus();
        //将订阅者从管理站中移除
        RxSubscriptions.remove(mSubscription);
    }
}
